/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.userpreference;


import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sdb.SDBFactory;
import com.hp.hpl.jena.sdb.Store;
import com.hp.hpl.jena.sdb.StoreDesc;
import com.hp.hpl.jena.sdb.sql.JDBC;
import com.hp.hpl.jena.sdb.sql.SDBConnection;
import com.hp.hpl.jena.sdb.store.DatabaseType;
import com.hp.hpl.jena.sdb.store.LayoutType;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.yonsei.iwb.smarttv.jena.ext.JenaModelType;
import edu.yonsei.iwb.smarttv.model.measure.PropertyWeightSet;
import edu.yonsei.iwb.smarttv.model.measure.ResourceWeights;
import edu.yonsei.iwb.smarttv.model.measure.WeightModel;
import edu.yonsei.iwb.smarttv.util.SMARTTVLog;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author JDK
 */
public class UserPreferenceModel {
    private Model upModel;
    private WeightModel pwm;
    private int ontType = UserPreferenceModelType.TDB_MODEL;
    private Store store;
    
    public UserPreferenceModel(int type) {
        this.ontType = type;
        if (type == UserPreferenceModelType.MEMORY_MODEL) {
            this.upModel = ModelFactory.createDefaultModel();
        } else if (type == UserPreferenceModelType.TDB_MODEL) {
            this.upModel = TDBFactory.createModel(UserPreferenceModelType.MODEL_DIR);
        } else if (type == UserPreferenceModelType.SDB_MODEL) {
            StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.Derby);
            JDBC.loadDriverDerby();
            SDBConnection conn = new SDBConnection(UserPreferenceModelType.DB_URL, UserPreferenceModelType.DB_USER, UserPreferenceModelType.DB_PASSWORD);
            this.store = SDBFactory.connectStore(conn, storeDesc);
            this.upModel = SDBFactory.connectDefaultModel(store);
        }
    }

    public void loadOntology(String fileName) {
        if (this.ontType == UserPreferenceModelType.SDB_MODEL) {
            this.store.getTableFormatter().create();
            this.store.getTableFormatter().truncate();
        } else if(this.ontType == UserPreferenceModelType.TDB_MODEL) {
            this.upModel.removeAll();
        }        
        InputStream in = FileManager.get().open(fileName);
        this.upModel.read(in, null);
        
        SMARTTVLog.log(SMARTTVLog.DETAIL, "Preference Ontology Loaded.\n");
        if (this.ontType == UserPreferenceModelType.TDB_MODEL) {
            upModel.close();
        }
    }
    
    public Model getUserPreferenceModel(){
        if (this.ontType == UserPreferenceModelType.TDB_MODEL) {
            this.upModel = TDBFactory.createModel(UserPreferenceModelType.MODEL_DIR);
        }
        return this.upModel;
    }
        
    /**
     * update된 온톨로지 모델을 파일로 작성함
     * @param filename 작성할 온톨로지 파일명
     */
    public void writeOntologyModel(String filename){
        
    }
    
    /**
     * rset에 들어있는 리소스들의 온톨로지 모델상의 Count수를 늘리기 위하여 트리플을 생성하여 jena model에 추가함
     * @param rset 업데이트 해야할 리소스 집합
     */
    public void preferedResourceCountUpdate(Vector<Resource> rset){
        
    }
    
    public void setWeightModel(ResourceWeights pr, Hashtable<Property, PropertyWeightSet> pws){
        if(this.pwm == null){
            this.pwm = new WeightModel(pr, pws);
        }            
        this.pwm.updateWeightModelByUser(pr, pws);
    }
    
    public WeightModel getWeightModel(){
        return this.pwm;
    }
    
    
}
